package jspservlet.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

//import jspservlet.db.DBConnect;
import jspservlet.dao.DeviceDAO;
import jspservlet.vo.Lamp;
import jspservlet.vo.Door;
import jspservlet.vo.Fan;
import jspservlet.vo.Device;
import jspservlet.vo.Connection;
import jspservlet.vo.AutoSunVisor;
import jspservlet.vo.FireSafety;

import org.json.*;

public class DeviceDAOImpl implements DeviceDAO{

	public int registeDevice(Device device)throws Exception{
	//输入电话号，设备号注册设备
		int flag = 1;
		return flag;
	}
	
	public int changeDoorpsw(Door door) throws Exception{
		
		Connection conn=new Connection();
		JSONObject TEST=new JSONObject();

		TEST.put("route","changedoorpsw");
		JSONObject test=new JSONObject();
		test.put("deviceID",door.getDeviceid());
		test.put("changePsw",door.getPsw());
		TEST.put("data",test);
		JSONObject returnMsg=conn.transport(TEST);

		int success=returnMsg.getInt("success");
		return success;

	}

	public int changeDoorstate(Door door) throws Exception{
		Connection conn=new Connection();
		JSONObject TEST=new JSONObject();

		TEST.put("route","changedoorstate");
		JSONObject test=new JSONObject();
		test.put("deviceID",door.getDeviceid());
		test.put("changeDoorState",door.getIson());

		TEST.put("data",test);
		JSONObject returnMsg=conn.transport(TEST);

		int success=returnMsg.getInt("success"); //改变门的状态成功
		return success;
	}

	public int changeLampLumi(Lamp lamp) throws Exception{
		Connection conn=new Connection();
		JSONObject TEST=new JSONObject();

		TEST.put("route","changelevel");
		JSONObject test=new JSONObject();
		test.put("deviceID",lamp.getDeviceid());
		test.put("changeLevel",Integer.parseInt(lamp.getGear()));

		TEST.put("data",test);
		JSONObject returnMsg=conn.transport(TEST);

		int success=returnMsg.getInt("success"); //改变门的状态成功
		return success;

	}
	public int changeLampColor(Lamp lamp) throws Exception{
		
		Connection conn=new Connection();
		JSONObject TEST=new JSONObject();

		TEST.put("route","changelight");
		JSONObject test=new JSONObject();

		test.put("deviceID",lamp.getDeviceid());
		test.put("changeLight",Integer.parseInt(lamp.getGear()));
		test.put("R",lamp.getRed());
		test.put("G",lamp.getGreen());
		test.put("B",lamp.getBlue());

		TEST.put("data",test);
		JSONObject returnMsg=conn.transport(TEST);

		int success=returnMsg.getInt("success"); 
		return success;

	}

	public int changeFanGear(Fan fan) throws Exception{
		Connection conn=new Connection();
		JSONObject TEST=new JSONObject();

		TEST.put("route","changefanstate");
		JSONObject test=new JSONObject();
		test.put("deviceID",fan.getIdDevice());
		test.put("changeFanState",fan.getGear());

		TEST.put("data",test);
		JSONObject returnMsg=conn.transport(TEST);

		int success=returnMsg.getInt("success"); 
		return success;
	}

	public int changeFanTem(Fan fan) throws Exception{
		Connection conn=new Connection();
		JSONObject TEST=new JSONObject();

		TEST.put("route","changefanopentemp");
		JSONObject test=new JSONObject();
		test.put("deviceID",fan.getIdDevice());
		test.put("changeFanOpenTemp",fan.getStemp());

		TEST.put("data",test);
		JSONObject returnMsg=conn.transport(TEST);

		int success=returnMsg.getInt("success"); 
		return success;
	}

	public int changeSunIson(AutoSunVisor autoSunVisor) throws Exception{
		Connection conn=new Connection();
		JSONObject TEST=new JSONObject();

		TEST.put("route","changecoverstate");
		JSONObject test=new JSONObject();
		test.put("deviceID",autoSunVisor.getIdDevice());
		test.put("changeCoverState", autoSunVisor.getIsOpen());

		TEST.put("data",test);
		JSONObject returnMsg=conn.transport(TEST);

		int success=returnMsg.getInt("success"); 
		return success;
	}

	public int changeSunGear(AutoSunVisor autoSunVisor) throws Exception{
		Connection conn=new Connection();
		JSONObject TEST=new JSONObject();

		TEST.put("route","changecoveropenillu");
		JSONObject test=new JSONObject();
		test.put("deviceID",autoSunVisor.getIdDevice());
		test.put("changeCoverOpenIllu",Float.parseFloat(autoSunVisor.getSbrightness()) );

		TEST.put("data",test);
		JSONObject returnMsg=conn.transport(TEST);

		int success=returnMsg.getInt("success"); 
		return success;

	}

	public int changeMode(FireSafety fireSafety) throws Exception{
		Connection conn=new Connection();
		JSONObject TEST=new JSONObject();

		TEST.put("route","changesecmode");
		JSONObject test=new JSONObject();
		test.put("deviceID",fireSafety.getDeviceid());
		test.put("changeSecMode",fireSafety.getAlarm() );

		TEST.put("data",test);
		JSONObject returnMsg=conn.transport(TEST);

		int success=returnMsg.getInt("success"); 
		return success;



	}
}
